<?php
namespace app\models;
use yii\base\Model;
use common\component\tool\Tool;


class MessageSearch extends Message
{

    public $begin_time;
    public $end_time;

    public function rules()
    {
        return [
            ['app_id', 'integer', 'message' => 'state必须为数字，或数字字符串'],
            ['reply', 'integer', 'message' => 'reply必须为数字，或数字字符串'],
            ['reply', 'in', 'range' => array(1, 2), 'message' => 'reply值必须在1~2中选择'],
            ['begin_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'begin_time非yyyy-MM-dd格式'],
            ['end_time', 'date', 'format'=>'yyyy-MM-dd', 'message' => 'end_time非yyyy-MM-dd格式']
        ];
    }


    public function scenarios()
    {
        return Model::scenarios();
    }

    public function search($params)
    {
        $data['MessageSearch'] = $params;
        // P($data);

        $query = Message::find();

        if (!($this->load($data) && $this->validate())) {
            // if($this->hasErrors()){
            //     P($this->getErrors());
            // }
            return false;
        }
        // P($this);
        if(isset($this->app_id)){
            $query -> andWhere([
                'app_id' => $this->app_id,
            ]);            
        }


        if(isset($this->reply)){
            $query -> andFilterWhere([
                'reply' => $this->reply,
            ]);
        }


        if(isset($this->end_time) and !empty($this->end_time)){
            $this->end_time .= ' 23:59:59';
        }
        $query -> andFilterWhere(['>=', 'add_time', $this->begin_time])
               -> andFilterWhere(['<=', 'add_time', $this->end_time]);

        // P($query);
        return $query;
    }

    
}